<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-24
 * Time: 15:24
 */

namespace app\api\service\order;


use app\api\model\CouponUse;
use app\api\model\MoneyOrder;
use app\api\model\StoreOrder;
use app\api\model\User;
use app\api\model\UserMoney;
use think\Db;
use think\Exception;
use think\facade\Log;

class StoreOrderSer
{
    // 订单
    private $order;

    // 用户信息
    private $user;

    private $price;

    function __construct($order)
    {
        $this->order = $order;
        $this->price = round($this->order['price'] - $this->order['coupon_price'],2);
        $this->user = User::get($this->order['user_id']);
    }


    /**
     * 更改用户状态
     */
    public function changeStatus()
    {
        if($this->order['status'] == 3) {
            Db::startTrans();
            try
            {
                // 更新订单状态
                $this->updateOrder();
                Db::commit();
                return true;
            }
            catch (Exception $ex)
            {
                Db::rollback();
                Log::init([
                    'type' => 'File',
                    'path' => LOG_PATH,
                    'level' => ['error']
                ]);
                Log::record($ex,'error');
                return false;
            }
        } else {
            return true;
        }
    }

    /**
     * 更新状态
     */
    private function updateOrder()
    {
        if($this->order['payment'] == 1) {
            $StoreOrder = new StoreOrder();
            $StoreOrder->save([
                'status' => 4,
                'pay_time' => time(),
                'price' => $this->price
            ],[ 'id' => $this->order['id'] ]);
            if($this->order['coupon_use_id'] > 0) {
                $couponUse = new CouponUse();
                $couponUse->save([
                    'status' => 2
                ],[ 'id' => $this->order['coupon_use_id'] ]);
            }
        } else {
            if($this->price <= $this->user['money']) {
                $StoreOrder = new StoreOrder();
                $StoreOrder->save([
                    'status' => 4,
                    'pay_time' => time(),
                    'price' => $this->price
                ],[ 'id' => $this->order['id'] ]);
                if($this->order['coupon_use_id'] > 0) {
                    $couponUse = new CouponUse();
                    $couponUse->save([
                        'status' => 2
                    ],[ 'id' => $this->order['coupon_use_id'] ]);
                }
                $money = $this->price;
                UserMoney::create([
                    'money' => $money,
                    'user_id' => $this->user['id'],
                    'type' => 2,
                    'user_money' => $this->user['money'],
                    'order_id' => $this->order['id']
                ]);
                User::where('id',$this->user['id'])->update([
                    'money' => round($this->user['money'] - $money,2)
                ]);
            }
        }
    }
}